import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;

public class BusScreen extends Screen
{
	private int defaultDay;
	private ArrayList<Hotspot> busHotspots;
	private ArrayList<Hotspot> dayHotspots;
	private Screen bus1Screen, bus2Screen, bus3Screen;
	private Screen day1Screen, day2Screen, day3Screen;
	private Screen busMainScreen; 
	private Screen currBusScreen, currDayScreen;
	
	public BusScreen()
	{
		// Call parent
		super("imgs/top_s1.png");
		
		// Set today
		Calendar now = Calendar.getInstance();
		if (now.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
		{
			defaultDay = 1;
		}
		else if (now.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
		{
			defaultDay = 2;
		}
		else
		{
			defaultDay = 0;
		}
		
		// Initialize things
		busHotspots = new ArrayList<Hotspot>();
		dayHotspots = new ArrayList<Hotspot>();
		
		// Load subscreens
		bus1Screen = new Screen("imgs/bus_menu2_s1.png");
		bus2Screen = new Screen("imgs/bus_menu2_s2.png");
		bus3Screen = new Screen("imgs/bus_menu2_s3.png");
		day1Screen = new Screen("imgs/menu3_s1.png");
		day2Screen = new Screen("imgs/menu3_s2.png");
		day3Screen = new Screen("imgs/menu3_s3.png");
		busMainScreen = new Screen("imgs/bus_screen_s1.png");
		
		// Set screen positions
		bus1Screen.setPosition(129, 88);
		bus2Screen.setPosition(129, 88);
		bus3Screen.setPosition(129, 88);
		day1Screen.setPosition(222, 88);
		day2Screen.setPosition(222, 88);
		day3Screen.setPosition(222, 88);
		busMainScreen.setPosition(222, 145);
		
		// Set hotspots
		busHotspots.add(new Hotspot(135, 198, 233, 295, bus1Screen));
		busHotspots.add(new Hotspot(135, 317, 233, 414, bus2Screen));
		busHotspots.add(new Hotspot(135, 438, 233, 535, bus3Screen));
		dayHotspots.add(new Hotspot(243, 95, 463, 184, day1Screen));
		dayHotspots.add(new Hotspot(512, 95, 732, 184, day2Screen));
		dayHotspots.add(new Hotspot(780, 95, 1000, 184, day3Screen));
		
		// Set initial screens~
		currBusScreen = bus1Screen;
		setDayScreenFromDefault();
	}
	
	public void selectBus(int i)
	{
		if (i == 0) currBusScreen = bus1Screen;
		if (i == 1) currBusScreen = bus2Screen;
		if (i == 2) currBusScreen = bus3Screen;
	}
	
	public int getSelectedDay()
	{
		if (currDayScreen == day1Screen) return 0;
		if (currDayScreen == day2Screen) return 1;
		if (currDayScreen == day3Screen) return 2;
		else return 0;
	}
	
	public int getSelectedBus()
	{
		
		if (currBusScreen == bus1Screen) return 0;
		if (currBusScreen == bus2Screen) return 1;
		if (currBusScreen == bus3Screen) return 2;
		else return 0;
	}
	
	public void setDayScreenFromDefault()
	{
		switch(defaultDay)
		{
			case 0:
				currDayScreen = day1Screen;
				break;
			case 1:
				currDayScreen = day2Screen;
				break;
			case 2:
				currDayScreen = day3Screen;
				break;
			default:
				break;
		}
	}
	
	@Override
	public void draw(Graphics g)
	{
		g.fillRect(129, 0, 1024, 720);
		g.drawImage(bg, 129, 0, null);
		currBusScreen.draw(g);
		currDayScreen.draw(g);
		busMainScreen.draw(g);
		
		
	}
	
	@Override
	public void checkClick(int x, int y)
	{
		// Check against both hotspot lists
		for (int i=0; i<busHotspots.size(); ++i)
		{
			Hotspot h = busHotspots.get(i);
			if (h.contains(x, y))
			{
				// Clicked on a bus
				currBusScreen = h.screen;
				setDayScreenFromDefault();
			}
		}
		for (int i=0; i<dayHotspots.size(); ++i)
		{
			Hotspot h = dayHotspots.get(i);
			if (h.contains(x, y))
			{
				// Clicked on a day
				currDayScreen = h.screen;
			}
		}
	}
}